<?php 
if(!defined('DREAMSCAPE')){die('Sorry, but this file cannot be directly viewed.');} 
/*   Themes   //-------*/
add_admin_menu(L_ADMIN_EDIT_THEMES,array('<a href="?cat=admin&amp;sub=theme_editor">'.L_ADMIN_EDIT_THEMES.'</a>'),'themeEditor');

if($admin_sub !== 'theme_editor'){return;}
$themename = check_get_alphanum('theme');
if(!empty($themename)){
				$theme = !empty($themename) ? THEMES.'/'.$themename : '';
				$theme = str_replace($special_chars, '', $theme);
				$theme = realpath($theme);
				$themes = getFiles($themesdir, 'dirs');
				if(!empty($theme) && is_dir($theme) && basename($theme) != 'themes' && in_array($themename, $themes)){
				$theme_css_file = file_exists($theme.'/css/'.$themename.'.css') 
									? $theme.'/css/'.$themename.'.css' 
									: $theme.'/css/styles.css';
				$theme_info = theme_info($theme_css_file);
				$crumb = has_theme_info($theme_info) ? $theme_info->Title : $themename;
				$crumb = sprintf(L_CURRENTLY_EDITING, $crumb);
				add_breadcrumb('<a href="index.php?cat=admin&sub=theme_editor">'.L_THEME_EDITOR_TITLE.'</a>');
				add_title(L_THEME_EDITOR_TITLE);
				add_title($crumb);
				add_breadcrumb($crumb);
				}
} else {
add_breadcrumb(L_THEME_EDITOR_TITLE);
add_title(L_THEME_EDITOR_TITLE);
}
ozone_action('admin_page', 'theme_content');
function theme_content(){
global $output, $special_chars, $themesdir, $filearr, $auth;
	if(is_posting(L_BUTTON_ACTIVATE) && isset($_POST['activate_this']) && !empty($_POST['activate_this'])){
		$activate_theme = preg_replace('([^[:alnum:]_[:space:]])', '',  trim($_POST['activate_this']));
		if(setOption('theme',$activate_theme)){
		printOut(SUCCESS, L_THEME_ACTIVATE_SUCCESS);
		} else {
		printOut(FAILURE, L_THEME_ACTIVATE_FAILURE);
		}
	}

	if(isset($_POST['new_submit']) && !empty($_POST['new_theme_name'])){
	
			$new_theme_name = $proper_theme_name = preg_replace('([^[:alnum:]_[:space:]])', '',  $_POST['new_theme_name']);
			$new_theme_name = str_replace(' ', '_',  $new_theme_name);
			$errors = array();
			$tplext = TPL_EXT;
			$dreamscape_dir = DREAMSCAPEPATH;
			$theme_dir = "$dreamscape_dir/themes/$new_theme_name";
			$yoursite = YOUR_SITE;
			if(!file_exists($theme_dir)){
			copyr("$dreamscape_dir/funcs/misc/theme_prototype/", $theme_dir);
			$style_sheet = file_get_contents("$theme_dir/css/styles.css");
			$users = new Dreamscape('users');
			$users->Get($auth->Id);
$style_sheet = str_replace('/*__CSS_META__*/', 
'/*
Theme Name: '.$proper_theme_name.'
Theme URL: '.$yoursite.'
Description: Courteously provided by your friends at '.COMPANY_NAME.' We heart your face.
Version: 1.0
Author: '.((!empty($auth->DisplayName)) ? $auth->DisplayName : $auth->Username).'
Author URL: '.((isset($users->url) && !empty($users->url)) ? $users->url : $yoursite).'
*/', $style_sheet);
			$fp = fopen("$theme_dir/css/styles.css", 'w+');
			fwrite($fp, $style_sheet);
			fclose($fp);
			if(empty($errors)){
			printOut(SUCCESS,L_THEME_CREATED);
			} else {
			printOut(FAILURE,L_THEME_FAILURE);
			}
		} else {
			printOut(FAILURE,L_THEME_FAILURE);
		}
	}

$themename = isset($_GET['theme']) ? $_GET['theme'] : '';
$themename = str_replace($special_chars, '', $themename);
$themename = str_replace('.', '', $themename);
$themes = getFiles($themesdir, 'dirs');
$theme = !empty($themename) ? DREAMSCAPEPATH.'/'.$themesdir.'/'.$themename : '';
$theme = realpath($theme);
$themefile = isset($_GET['themefile']) ? $_GET['themefile'] : '';
$themefile = str_replace($special_chars, '', $themefile);
$writable = is_writable($theme) ? true : false;
if (!empty($theme) && is_dir($theme) && basename($theme) != 'themes' && in_array($themename, $themes)){
if(!$writable){
printOut(ALERT,L_THEME_NO_PERMISSIONS);
}
if($writable){
if(is_posting(L_BUTTON_UPDATE)){
	
	$filename =(!empty($themefile) && is_file($theme.'/'.$themefile)) ? $theme.'/'.$themefile : "$theme/css/$themename.css";
		$file_contents = $_POST['file_contents'];
		$file_contents = applyOzoneAction('theme_update_file', $file_contents);
		if(is_file($filename)){
		$fp = fopen($filename,'wb');	
		if(fwrite($fp,$file_contents)){
			printOut(SUCCESS, L_THEME_FILE_UPDATED);
		}
		}
		
		fclose($fp);
	
}
if(is_posting(L_BUTTON_CREATE)){
		$create_in = isset($_POST['create_in']) && $_POST['create_in'] != 'main' ? $theme.'/'.$_POST['create_in']: $theme;
		$create_in_base = isset($_POST['create_in']) && $_POST['create_in'] == 'main' ? '' : $_POST['create_in'].'%2F';//
		$filename = isset($_POST['filename']) ? trim($_POST['filename']) : '';
		$filename = str_replace($special_chars, '', $filename);
		
		$filename = str_replace('/', '', $filename);
		$filename = $filename != '.' && $filename != '..' ? $filename : '';
		$file_contents = sprintf(L_THEME_FILE_CREATE_NOTE, $filename, date('Y'));
		
		$create_in = str_replace($special_chars, '', $create_in);
		$filepath = $create_in.'/'.$filename;
		/*echo  $create_in_base;*/
		
		if(!empty($filename)){
			if(!file_exists($filepath) && !is_file($filepath)){
				$fp = fopen($filepath,'w+');	
				if(fwrite($fp,$file_contents)){
					printOut(SUCCESS, sprintf(L_THEME_FILE_CREATED,$themename,$create_in_base.$filename));
				}
				fclose($fp);
			} else {
				printOut(FAILURE, L_THEME_EXISTING_FILE);
			}
			
			
		} else {
			printOut(FAILURE, L_THEME_MISSING_FILENAME);
		}
		
	
}
}
$themes = getFiles($themesdir, 'all', 1);

?>
</form>
<div id="themeEditor">
<?php echo $output ;?>
<div class="stretchContainer">
<h3 class="stretchToggle" id="wedge"><span><?php echo L_THEME_CREATE_NEW_FILE ?></span></h3>
<form action="" method="post" class="stretch">
<label for="create_in"><?php echo L_THEME_CREATE_IN_FOLDER ?></label>
<select name="create_in" id="create_in" class="infields">
<option value="main"><?php echo $themename; ?></option>
<?php 


foreach($themes['dirs'][$themename]['dirs'] as $k => $v){
if(!in_array($k, $blocked_dirs)){
?>
<option value="<?php echo $k ?>">&mdash; <?php echo $k ?></option>
<?php
}
}	


?>
</select><br />
<label for="filename"><?php echo L_THEME_CREATE_FILENAME ?></label><input type="text" value="" id="filename" name="filename"  /><br />

<label for="submit"></label><input id="submit" type="submit" name="submit" value="<?php echo L_BUTTON_CREATE ?>" /><br />
</form></div>

<form method="get" action="">
<input name="cat" type="hidden" value="admin" />
<input name="sub" type="hidden" value="theme_editor" />
<label for="theme"><?php echo L_THEME_SELECT_THEME ?></label><select name="theme" id="theme">
<?php 
foreach($themes['dirs'] as $k => $val){
	if($k != $themename){
?>
<option value="<?php echo $k ?>"><?php echo str_replace('_', ' ', $k); ?></option>
<?php	
	} else {
?>
<option selected="selected" value="<?php echo $k ?>"><?php echo str_replace('_', ' ', $k); ?></option>
<?php	
	}

}
?>


</select><br />

<label for="themefile"><?php echo L_THEME_SELECT_FILE ?></label><select id="themefile" name="themefile">
<?php 
ksort($themes['dirs'][$themename]['dirs']);
foreach($themes['dirs'] as $ind => $val){
if($ind == $themename){


?>
<?php 

foreach($val['dirs'] as $k => $v){
if(!in_array($k, $blocked_dirs)){
?>
<optgroup label="<?php echo $k; ?>">
<?php
asort($v['files']);
foreach($v['files'] as $files){ 
if($themefile != $k.'/'.$files){
?>
<option value="<?php echo $k.'/'.$files; ?>"><?php echo $files; ?></option>
<?php 
} else {
?>
<option selected="selected" value="<?php echo $k.'/'.$files; ?>" class="current">&raquo; <?php echo $files; ?></option>
<?php 
}

}
?>
</optgroup>
<?php 
}


}
if(!empty($val['files'])){
?>
<optgroup label="<?php echo L_THEME_MAIN_FOLDER ?>">
<?php
	foreach($val['files'] as $mfiles){
		if($themefile != $mfiles){
		?>
		<option value="<?php echo $mfiles; ?>"><?php echo $mfiles; ?></option>
		<?php 
		} else {
		?>
		<option selected="selected" value="<?php echo $mfiles; ?>">&raquo; <?php echo $mfiles; ?></option>
		<?php 
		}
	}
}
?>
</optgroup>
<?php
}
}

?>
</select><br />
<label for="submit"></label><input id="submit" type="submit" value="<?php echo L_BUTTON_EDIT ?>" /><br />
</form>


<form action="" method="post" accept-charset="utf-8">
<?php 
$filename = urldecode($theme.'/'.$themefile);
if(!empty($themefile) && is_file($filename)){
$filecontents = $filename;
$properfilename = $themename.'/'.$themefile;
} else {
$filecontents = "$theme/css/$themename.css";

$filecontents = file_exists("$theme/css/$themename.css") 
			  ? "$theme/css/$themename.css" 
			  : "$theme/css/styles.css";
$properfilename = file_exists("$themename/css/$themename.css") 
				? "$themename/css/$themename.css"
				: "$themename/css/styles.css";
}
$filecontents = is_readable($filecontents) ? file_get_contents($filecontents) : L_FILE_NOT_READABLE;
$filecontents =  view($filecontents);
?>
<h2>Editing <?php echo $properfilename;  ?></h2>
<?php if(CUSTOM_INSTALL != true && preg_match('/\.tpl\.html$/', $themefile)){printf(NOTE.'<br />',L_THEME_EDITOR_VAR_NOTE); }?>

<textarea name="file_contents"><?php echo $filecontents;  ?></textarea>
<input type="submit" name="submit" id="submit" value="update" />
<?php
} else { ?>
<div id="themeEditor">
<?php echo $output ;?>
<p class="contentnote"><?php echo L_THEME_EDIT_CHOOSE_NOTE ?></p>
<table id="themeList">
<?php 
$dreamscape_dir = DREAMSCAPEPATH;
$yoursite = YOUR_SITE;
$active_theme = getOption('theme');
foreach($themes as $ind => $theme){
$preview_img = "$dreamscape_dir/themes/$theme/images/preview.png";
$preview_img_url = DREAMSCAPE_URL."themes/$theme/images/preview.png";
?>
<tr<?php echo ($theme == $active_theme) ?  ' class="activeTheme"' : (($ind % 2) ? ' class="altRow"' : ''); ?>>
<td>
<?php 
if(file_exists($preview_img)){
echo '<img src="'.$preview_img_url.'" />';
}
 ?>
</td>
<td class="themeInfo">
<?php 
$theme_css_file = file_exists("$dreamscape_dir/themes/$theme/css/$theme.css") 
			  ? "$dreamscape_dir/themes/$theme/css/$theme.css"
			  : "$dreamscape_dir/themes/$theme/css/styles.css";
$theme_info = theme_info($theme_css_file);
if(has_theme_info($theme_info)){
?>
<h1><?php echo !empty($theme_info->Name) ? $theme_info->Name : str_replace('_', ' ', $theme); ?></h1>
<?php 
echo !empty($theme_info->Author) ? '<h2>'.L_THEME_BY.' '.$theme_info->Author.'</h2>' : '';
echo !empty($theme_info->Version) ? '<h3>'.L_THEME_VERSION.' '.$theme_info->Version.'</h3>' : '';
echo !empty($theme_info->Description) ? '<p>'.$theme_info->Description.'</p>' : '';
} else {
echo '<h1>'.str_replace('_', ' ', $theme).'</h1>';
}
?>
</td><td><a href="<?php echo $yoursite ?>?theme=<?php echo $theme ?>" target="_blank"><?php echo L_THEME_PREVIEW ?></a></td><td><a href="index.php?cat=admin&amp;sub=theme_editor&amp;theme=<?php echo $theme ?>" class="editLink"><?php echo L_THEME_EDIT_TEXT ?></a></td><td>
<?php 
if($theme != $active_theme){
?>
<label for="activate_this_<?php echo $ind?>"><?php echo L_THEME_ACTIVATE_TEXT ?></label><input type="radio" id="activate_this_<?php echo $ind?>" name="activate_this" value="<?php echo $theme; ?>" /><br />


<?php 
} else {
echo 'Active Theme';
}
?></td></tr>
<?php
}
?>                   
</table>
<label for="submit"></label><input type="submit" name="submit" id="submit" value="<?php echo L_BUTTON_ACTIVATE ?>" /><br />

<?php 
if(is_writable(DREAMSCAPEPATH.'/themes')){
?>
<fieldset>
<legend><?php echo L_THEME_CREATE_TITLE ?></legend>
<p><?php echo L_THEME_CREATE_DETAIL ?></p>
<label for="new_theme_name"><?php echo L_THEME_CREATE_THEME_NAME ?></label><input name="new_theme_name" id="new_theme_name" type="text" /><br />
<label for="new_submit"></label><input value="<?php echo L_BUTTON_MAKE_NEW_THEME ?>" type="submit" name="new_submit" id="new_submit" /><br />
</fieldset>
<?php 
}
?>
</div>
<?php
}
}
?> 		